package util

import (
	"strings"

	"mall/service/backend/rpc/backendclient"
	"mall/service/user/rpc/userclient"
)

const (
	arrayValueSeparator    = ","
	keyValueSeparator      = ":"
	keyOpSeparator         = ","
	arrayKeyValueSeparator = ";"
)

func ParseBackendSorter(sorter string) (res []*backendclient.OrderBy) { //////
	sorters := strings.Split(sorter, arrayKeyValueSeparator)
	if len(sorters) > 0 {
		for _, orderBy := range sorters {
			orderBys := strings.SplitN(orderBy, keyValueSeparator, 2)
			if len(orderBys) == 2 && orderBys[0] != "" && orderBys[1] != "" {
				res = append(res, &backendclient.OrderBy{
					Field: orderBys[0],
					Order: orderBys[1],
				})
			}
		}
	}

	return
}

func ParseBackendFilter(filter string) (res []*backendclient.FilterBy) { //////
	filters := strings.Split(filter, arrayKeyValueSeparator)
	if len(filters) > 0 {
		for _, filterBy := range filters {
			filterBys := strings.SplitN(filterBy, keyValueSeparator, 2)
			if len(filterBys) == 2 && filterBys[0] != "" && filterBys[1] != "" {
				res = append(res, &backendclient.FilterBy{
					Field: filterBys[0],
					In:    filterBys[1],
				})
			}
		}
	}

	return
}

func ParseSorter(sorter string) (res []*userclient.BackendOrderBy) { //////
	sorters := strings.Split(sorter, arrayKeyValueSeparator)
	if len(sorters) > 0 {
		for _, orderBy := range sorters {
			orderBys := strings.SplitN(orderBy, keyValueSeparator, 2)
			if len(orderBys) == 2 && orderBys[0] != "" && orderBys[1] != "" {
				res = append(res, &userclient.BackendOrderBy{
					Field: orderBys[0],
					Order: orderBys[1],
				})
			}
		}
	}

	return
}

func ParseFilter(filter string) (res []*userclient.BackendFilterBy) { //////
	filters := strings.Split(filter, arrayKeyValueSeparator)
	if len(filters) > 0 {
		for _, filterBy := range filters {
			filterBys := strings.SplitN(filterBy, keyValueSeparator, 2)
			if len(filterBys) == 2 && filterBys[0] != "" && filterBys[1] != "" {
				res = append(res, &userclient.BackendFilterBy{
					Field: filterBys[0],
					In:    filterBys[1],
				})
			}
		}
	}

	return
}

func GetAssetTypeText(assetType int32) (res string) { //////
	switch assetType {
	case 1:
		res = "现金"
	case 2:
		res = "宝石"
	case 3:
		res = "积分"
	}

	return
}

func GetSourceTypeText(sourceType int32) (res string) { //////
	switch sourceType {
	case 0:
		res = "消费"
	case 1:
		res = "充值"
	case 2:
		res = "退款"
	case 3:
		res = "售后"
	case 4:
		res = "售出闲置"
	case 5:
		res = "提现"
	}

	return
}

func GetTransactionStatusText(status int32) (res string) { //////
	switch status {
	case 1:
		res = "正常"
	case 3:
		res = "已删除"
	default:
		res = "其他"
	}

	return
}

func GetStatusText(status int32) (res string) { //////
	switch status {
	case 1:
		res = "正常"
	case 3:
		res = "已删除"
	default:
		res = "其他"
	}

	return
}

func GetGenderText(gender int32) (res string) { //////
	switch gender {
	case 1:
		res = "男"
	case 2:
		res = "女"
	default:
		res = "其他"
	}

	return
}

type Option struct {
	CanDisable bool
	CanEnable  bool
	CanRemove  bool
	CanRestore bool
}

func GetOptionByStatus(status int32) (*Option) { //////
	res := &Option{}

	switch status {
	case 0:
		res.CanEnable = true
		res.CanRemove = true
	case 1:
		res.CanDisable = true
		res.CanRemove = true
	case 3:
		res.CanRestore = true
	}

	return res
}
